//
//  SpeTableViewController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/3/31.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DetailsSpecialTableViewController.h"
#import "SpeTableViewController.h"
#import "UIImageView+WebCache.h"
#import "specialModel.h"
#import "Singleton.h"
#import "SpecialCell.h"
#import "CDHttpHelper.h"

@interface SpeTableViewController ()
@property(nonatomic,strong)NSMutableArray * allDataArray;
@property(nonatomic,strong)NSMutableArray * IDArray;
@end

#define url @"https://chanyouji.com/api/articles.json?destination_id=55&page=1"

@implementation SpeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
     
     攻略下专题界面
     
     */
    
    
    self.allDataArray = [NSMutableArray array];
    self.IDArray = [NSMutableArray array];
    [self.tableView registerNib:[UINib nibWithNibName:@"SpecialCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Special"];
    
    [self handleData];
}

- (void)handleData{
    [[CDHttpHelper defaultCDHttpHelper] get:[NSString stringWithFormat:@"https://chanyouji.com/api/articles.json?destination_id=%ld&page=1",self.id] params:nil success:^(id responseObj) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dict in array) {
            specialModel *model = [specialModel new];
            [model setValuesForKeysWithDictionary:dict];
            [self.allDataArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allDataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Special" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[SpecialCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Special"];
    }
    
    specialModel *model = self.allDataArray[indexPath.row];
    
    cell.name.text = model.name;
    cell.title.text = model.title;
    
    cell.name.textColor = [UIColor whiteColor];
    cell.name.font = [UIFont systemFontOfSize:25];
    cell.title.textColor = [UIColor whiteColor];
    cell.title.font = [UIFont systemFontOfSize:20];
    
    [cell.image_url sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
    
    cell.image_url.layer.cornerRadius = 8;
    cell.image_url.layer.masksToBounds = YES;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailsSpecialTableViewController *DetailsSpecialVC = [[DetailsSpecialTableViewController alloc]init];
    specialModel *model = self.allDataArray[indexPath.row];
    
    Singleton *single = [Singleton shareSingle];
    single.specialID = model.specialID;
    
   
    
    [self.navigationController pushViewController:DetailsSpecialVC animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
